'use strict';
const { nanoid } = require('nanoid');
const bcrypt = require('bcrypt');
class Utils {
    constructor() {
    }
    /**
     * 获取两位的校验位
     */
    luhnChecksum(num) {
        if (typeof num === 'number') {
            num += "";
        }
        let sum = 0;
        let shouldDouble = false;

        for (let i = num.length - 1; i >= 0; i--) {
            let digit = parseInt(num[i], 10);
            if (shouldDouble) {
                digit *= 2;
                if (digit > 9) digit -= 9;
            }
            sum += digit;
            shouldDouble = !shouldDouble;
        }

        const checksum = (10 - (sum % 10)) % 100; // 校验位
        return checksum.toString().padStart(2, '0'); // 确保返回两位数的字符串
    }
    //生成密码的哈希值
    async hashPassword(password) {
        const saltRounds = 10; // 加盐强度（推荐值为 10）
        const hashedPassword = await bcrypt.hash(password, saltRounds);
        return hashedPassword;
    }
    //验证用户输入的密码是否匹配存储的哈希值
    async verifyPassword(inputPassword, hashedPassword) {
        const isMatch = await bcrypt.compare(inputPassword, hashedPassword);
        return isMatch;
    }
    /**
     * 验证token
     */
    generateNanoIdToken(length = 21) {
        return nanoid(length);
    }
    /**
     * 判断两个数组是否相同
     */
    areArraysEqual(arr1, arr2) {
        if (arr1.length !== arr2.length) return false;

        for (let i = 0; i < arr1.length; i++) {
            if (arr1[i] !== arr2[i]) return false;
        }

        return true;
    }
}
const utils = new Utils();
module.exports = utils;